package invokerV2;

import invoker.Ihm;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import receiver.MiniEditeurImpl;

import command.Command;

public class IhmImplV2 implements Ihm, Observer{

	private JFrame miniEditeurV2;

	private int debutSelection;

	private int finSelection;

	private String textInserer = new String();

	private HashMap<String, Command> listCommand;

	private JTextArea textAreaInserer;

	private JTextArea textAreaContenu;

	private JTextField textFieldSelection;

	private JPanel panelBoutons;

	private JScrollPane scrollPaneInserer;

	private JScrollPane scrollPaneContenuBuffer;

	private JButton btnCouper;

	private JButton btnColler;

	private JButton btnCopier;
	private JButton btnRejouer;
	private JButton btnArrter;
	private JButton btnDmarrer;
	private JPanel panelBoutonsV1;
	private JButton btnInsrer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IhmImplV2 window = new IhmImplV2();
					window.miniEditeurV2.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public IhmImplV2() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		miniEditeurV2 = new JFrame();
		miniEditeurV2.setTitle("Mini éditeur V2");
		miniEditeurV2.setBounds(100, 100, 562, 353);
		miniEditeurV2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panelBoutons = new JPanel();

		scrollPaneInserer = new JScrollPane();

		scrollPaneContenuBuffer = new JScrollPane();

		textFieldSelection = new JTextField();
		textFieldSelection.setAlignmentX(Component.LEFT_ALIGNMENT);
		textFieldSelection.setColumns(10);

		panelBoutonsV1 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(miniEditeurV2.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(panelBoutonsV1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(panelBoutons, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(textFieldSelection, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(scrollPaneContenuBuffer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
								.addComponent(scrollPaneInserer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE))
								.addContainerGap())
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(panelBoutons, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panelBoutonsV1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(scrollPaneInserer, GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
						.addGap(20)
						.addComponent(scrollPaneContenuBuffer, GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textFieldSelection, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
						.addGap(4))
				);

		btnInsrer = new JButton("Insérer");
		btnInsrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textInserer = textAreaInserer.getText();
				listCommand.get("SaisirOriginator").execute();
				textAreaInserer.setText("");
			}
		});
		panelBoutonsV1.add(btnInsrer);

		btnCouper = new JButton("Couper");
		btnCouper.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listCommand.get("CouperOriginator").execute();				
			}
		});
		panelBoutonsV1.add(btnCouper);

		btnColler = new JButton("Coller");
		btnColler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listCommand.get("CollerOriginator").execute();				
			}
		});
		panelBoutonsV1.add(btnColler);

		btnCopier = new JButton("Copier");
		btnCopier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listCommand.get("CopierOriginator").execute();				
			}
		});
		panelBoutonsV1.add(btnCopier);

		textAreaContenu = new JTextArea();
		textAreaContenu.setEditable(false);

		textAreaContenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				debutSelection = textAreaContenu.getSelectionStart();
				finSelection = textAreaContenu.getSelectionEnd();
				listCommand.get("SelectionnerOriginator").execute();
				System.out.println("Début  :  "+debutSelection + "   Fin  :  " + finSelection);
			}
		});

		scrollPaneContenuBuffer.setViewportView(textAreaContenu);

		textAreaInserer = new JTextArea("");

		scrollPaneInserer.setViewportView(textAreaInserer);

		btnDmarrer = new JButton("Démarrer");
		btnDmarrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listCommand.get("Demarrer").execute();
			}
		});
		panelBoutons.add(btnDmarrer);

		btnArrter = new JButton("Arrêter");
		btnArrter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listCommand.get("Arreter").execute();
			}
		});
		panelBoutons.add(btnArrter);

		btnRejouer = new JButton("Rejouer");
		btnRejouer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listCommand.get("Rejouer").execute();
			}
		});
		panelBoutons.add(btnRejouer);
		miniEditeurV2.getContentPane().setLayout(groupLayout);
		miniEditeurV2.setVisible(true);
	}

	@Override
	public String getTexteInserer() {
		return textInserer;
	}

	@Override
	public int getDebutSelection() {
		return debutSelection;
	}

	@Override
	public int getFinSelection() {
		return finSelection;
	}

	@Override
	public void update(Observable o, Object arg) {
		textAreaContenu.setText((String) arg);
	}

	public void setLesCommandes(HashMap<String, Command> lesCommandes) {
		this.listCommand = lesCommandes;
	}
}
